package bscorp.appbase.db;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by mengweima on 2017/8/21.
 */

public class M4Dao<T, ID> {
    private M4DatabaseHelper helper;
    private static M4Dao m4Dao;

    private M4Dao(Context context, String databaseName, int databaseVersion) {
        helper = M4DatabaseHelper.getInstance(context, databaseName, databaseVersion);
    }

    private M4Dao(Context context) {
        helper = M4DatabaseHelper.getInstance(context);
    }

    static public M4Dao getInstance(Context context) {
        if (m4Dao == null) {
            synchronized (M4Dao.class) {
                if (m4Dao == null) {
                    m4Dao = new M4Dao(context);
                }
            }
        }
        return m4Dao;
    }

    public M4DatabaseHelper getHelper() {
        return helper;
    }

    public Dao<T, ID> getDao(Class classes) throws SQLException {
        return this.getHelper().getDao(classes);
    }

    public int add(T t, Class clazz) throws SQLException {
        return getDao(clazz).create(t);
    }

    public int addList(List<T> list, Class clazz) throws SQLException {
        return getDao(clazz).create(list);
    }

    public List<T> getAll(Class clazz) throws SQLException {
        List<T> list = new ArrayList<>();
        list = getDao(clazz).queryForAll();
        return list;
    }

    public T get(ID id, Class clazz) throws SQLException {
        return getDao(clazz).queryForId(id);
    }

    /**
     * 通过表中属性值查找对应的数据
     *
     * @param tableCol     属性名
     * @param tableColVale 属性值
     * @param clazz        对应表
     * @return
     * @throws SQLException
     */
    public List<T> getByValue(String tableCol, Object tableColVale, Class clazz) throws SQLException {
        List<T> list = getDao(clazz).queryBuilder()
                .where().eq(tableCol, tableColVale)
                .query();
        return list;
    }

    /**
     * 通过表中属性值查找对应的数据
     *
     * @param maps  属性名 属性值
     * @param clazz 对应表
     * @return
     * @throws SQLException
     */
    public List<T> getByValues(Map<String, String> maps, Class clazz) throws SQLException {
        QueryBuilder queryBuilder = getDao(clazz).queryBuilder();
        Where where = queryBuilder.where();
        int i = 0;
        for (Map.Entry<String, String> entry : maps.entrySet()) {
            String key = entry.getKey().toString();
            String value = entry.getValue().toString();
            if (i == 0) {
                where.eq(key, value);
                i++;
            } else {
                where.and().eq(key, value);
            }
        }
        return where.query();
    }

    /**
     * 通过表中属性值删除对应的数据
     *
     * @param tableCol     属性名
     * @param tableColVale 属性值
     * @param clazz        对应表
     * @return
     * @throws SQLException
     */
    public void deleteByValue(String tableCol, String tableColVale, Class clazz) throws SQLException {
        Dao dao = getDao(clazz);
        DeleteBuilder deleteBuilder = dao.deleteBuilder();
        deleteBuilder.where().eq(tableCol, tableColVale);
        deleteBuilder.delete();
    }

    public int update(T t, Class clazz) throws SQLException {
        return getDao(clazz).update(t);
    }

    public int deleteById(ID id, Class clazz) throws SQLException {
        return getDao(clazz).deleteById(id);
    }

    public int delete(T t, Class clazz) throws SQLException {
        return getDao(clazz).delete(t);
    }

    public void clearTable(Class clazz) throws SQLException {
        Dao dao = getDao(clazz);
        DeleteBuilder deleteBuilder = dao.deleteBuilder();
        deleteBuilder.where().gt("id", 0);
        deleteBuilder.delete();
    }

    public void deleteByGtValue(String tableCol, int tableColVale, Class clazz) throws SQLException {
        Dao dao = getDao(clazz);
        DeleteBuilder deleteBuilder = dao.deleteBuilder();
        deleteBuilder.where().gt(tableCol, tableColVale);
        deleteBuilder.delete();
    }
}
